//
//  CarInfoController.m
//  BalanceCar
//
//  Created by 郑信鸿 on 17/6/20.
//  Copyright © 2017年 郑信鸿. All rights reserved.
//

#import "CarInfoController.h"
#import "ItemCellView.h"
#import "CarInfoItemView.h"
#import "SeparateLineView.h"
#import "UIView+shadow.h"
#import "BaseSendData.h"
#import "CarTravelModel.h"

@interface CarInfoController ()

@property(nonatomic, strong)UIView *topView;

@property(nonatomic, strong)UIView *listView;

@property(nonatomic, strong)NSMutableArray *listCellViews;

@property(nonatomic, strong)NSMutableArray *infoItemViews;

@property(nonatomic, strong)UILabel *nameLabel;

@property(nonatomic, weak) ItemCellView *battryItem;

@property(nonatomic, weak) ItemCellView *odoItem;


@end

@implementation CarInfoController{
    
    CGFloat _topViewHeight;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = XHLocalizedString(@"My scooter");
    self.listCellViews = [NSMutableArray array];
    self.infoItemViews = [NSMutableArray array];
    // Do any additional setup after loading the view.
    [self establisSubViews];
    [self bulidBottomView];
    [self assignTravelModel];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(travelDetail:) name:kPeripheraDidReadDataNotification object:nil];
    
    [BaseSendData sendTravelParamsOrder];
}



- (void)establisSubViews{
   
    _topViewHeight = 360;
    if (kUIScreenWidth == 320) {
        
        _topViewHeight = 300;
        
    }
    CGFloat viewGap = 12;
    self.topView = [[UIView alloc] init];
    self.topView.backgroundColor = [UIColor whiteColor];
    [self.topView addShadowToView];
    [self.view addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_offset(viewGap);
        make.left.mas_offset(viewGap);
        make.right.mas_offset(-viewGap);
        make.height.mas_offset(_topViewHeight);
        
    }];
    //中间线条
    SeparateLineView *lineView = [[SeparateLineView alloc] init];
    [_topView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.mas_offset(0);
        make.centerY.mas_offset(0);
        make.height.mas_offset(0.5);
        
    }];
    UIImageView *tipImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"carinfo_title"]];
    [_topView addSubview:tipImage];
    [tipImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_offset(0);
    }];
    UILabel *tipLabel = [[UILabel alloc] init];
    tipLabel.text =XHLocalizedString(@"This trip");
    [_topView addSubview:tipLabel];
    tipLabel.textColor = [UIColor colorFromHexCode:@"#666666"];
    tipLabel.font = [UIFont systemFontOfSize:12];
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_offset(0);
        make.centerX.mas_offset(12);
        
    }];
    NSArray *titles = @[XHLocalizedString(@"Riding time"),XHLocalizedString(@"Riding mileage"),XHLocalizedString(@"Average speed")];
    NSArray *iconNames = @[@"carinfo_funicon_time",@"carinfo_funicon_lc",@"carinfo_funicon_speed"];
    NSString *unit1 = [NSString stringWithFormat:@" %@",[SwitcherTool shareManager].unit];
    NSString *unit2 = [NSString stringWithFormat:@" %@/h",[SwitcherTool shareManager].unit];
    NSArray *units = @[@" h", unit1, unit2];
    for (int i = 0; i < titles.count; i++) {
        
        CarInfoItemView *infoView = [[CarInfoItemView alloc] initWithTitle:titles[i] iconName:iconNames[i] unit:units[i]];
        [self.topView addSubview:infoView];
        [self.infoItemViews addObject:infoView];
        
    }
    [self.infoItemViews mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_offset(_topViewHeight / 4);
        
    }];
    [self.infoItemViews mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:2 leadSpacing:0 tailSpacing:0];
    CGFloat viewWidth = kUIScreenWidth - viewGap * 2;
    CGFloat itemWidth = viewWidth / 3;
    SeparateLineView *line1 = [[SeparateLineView alloc] init];
    [self.topView addSubview:line1];
    SeparateLineView *line2 = [[SeparateLineView alloc] init];
    [self.topView addSubview:line2];
    NSArray *lines = @[line1,line2];
    [lines mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_offset(80);
        make.width.mas_offset(0.5);
        make.centerY.mas_offset(_topViewHeight / 4);
        
    }];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_offset(-itemWidth/2);
        
    }];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_offset(itemWidth /2);
    }];
    CGFloat topGap = (_topViewHeight / 2 - 114)/2;
    UIImageView *headImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"carinfo_logo"]];
    [_topView addSubview:headImage];
    [headImage mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_offset(topGap);
        make.centerX.mas_offset(0);
    }];
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.text = [NSString stringWithFormat:@"%@",[BalanceBuleManager shareManager].connectPeripheral.name];
    self.nameLabel.textColor = [UIColor colorFromHexCode:@"#666666"];
    self.nameLabel.font = [UIFont systemFontOfSize:16];
    [_topView addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(headImage.mas_bottom).offset(viewGap);
        make.centerX.mas_offset(0);
    }];
    
}


- (void)bulidBottomView{
    UIView *bottomView = [[UIView alloc] init];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    [bottomView addShadowToView];
    CGFloat gap = 12;
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.topView.mas_bottom).offset(gap);
        make.left.mas_offset(gap);
        make.right.mas_offset(-gap);
        make.height.mas_offset(140);
    }];
    ItemCellView *itemView1 = [[ItemCellView alloc] initWithImageName:@"carinfo_listicon_battery" title:XHLocalizedString(@"Battery status")];
    
    [bottomView addSubview:itemView1];
    self.battryItem = itemView1;
    ItemCellView *itemView2 = [[ItemCellView alloc] initWithImageName:@"carinfo_listicon_lc" title:XHLocalizedString(@"Riding mileage")];
    [bottomView addSubview:itemView2];
    self.odoItem = itemView2;
    NSArray *items = @[itemView1,itemView2];
    [items mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(16);
        make.right.mas_offset(-16);
    }];
    [items mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedSpacing:40 leadSpacing:20 tailSpacing:20];
    SeparateLineView *lineView = [[SeparateLineView alloc] init];
    [bottomView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.mas_offset(0);
        make.centerY.mas_offset(0);
        make.height.mas_offset(0.5);
        
    }];
    
}



- (void)travelDetail:(NSNotification *)notice{
    ResultModel *model = notice.object;
    if (model.dataType == TravelParamType) {
        
        [self assignTravelModel];
        
    }
    
}
- (void)assignTravelModel{
    
    CarInfoItemView *timeItem = self.infoItemViews[0];
    timeItem.value = [NSString stringWithFormat:@"%.1lf", [BaseAcceptData shareManager].travelModel.travelTime / 3600];
    CarInfoItemView *kmItem = self.infoItemViews[1];
    kmItem.value = [NSString stringWithFormat:@"%.1f",[BaseAcceptData shareManager].travelModel.DST];
    CarInfoItemView *speedItem = self.infoItemViews[2];
    if ([BaseAcceptData shareManager].travelModel.travelTime) {
        speedItem.value = [NSString stringWithFormat:@"%.lf", [BaseAcceptData shareManager].travelModel.DST / [BaseAcceptData shareManager].travelModel.travelTime * 3600];
    }else{
        
        speedItem.value = @"0.0";
        
    }
   
    self.battryItem.valueLabel.text = [NSString stringWithFormat:@"%d%%",[BaseAcceptData shareManager].travelModel.power];
    
    self.odoItem.valueLabel.text = [NSString stringWithFormat:@"%.1lf%@",[BaseAcceptData shareManager].travelModel.ODO,[SwitcherTool shareManager].unit];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
